package com.zmx.patients.fragment;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.itingchunyu.badgeview.BadgeTextView;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.youth.banner.Banner;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;
import com.zmx.patients.MyApplication;
import com.zmx.patients.R;
import com.zmx.patients.adapter.ArticleMessageAdapter;
import com.zmx.patients.adapter.RecommendedDoctorAdapter;
import com.zmx.patients.listener.InformUpdateListenerManager;
import com.zmx.patients.listener.MessageUpdateListener;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.ArticleMessage;
import com.zmx.patients.pojo.BraceletData;
import com.zmx.patients.pojo.Doctor;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.activity.AllDoctorActivity;
import com.zmx.patients.view.activity.ArticleDetailsActivity;
import com.zmx.patients.view.activity.BraceletDataActivity;
import com.zmx.patients.view.activity.COVIDActivity;
import com.zmx.patients.view.activity.CallEmergencyActivity;
import com.zmx.patients.view.activity.CheckDiseaseActivity;
import com.zmx.patients.view.activity.CircleOfFriendsActivity;
import com.zmx.patients.view.activity.CollectionDoctorActivity;
import com.zmx.patients.view.activity.CommunityActivity;
import com.zmx.patients.view.activity.DiseaseLibraryActivity;
import com.zmx.patients.view.activity.DiseaseSearchActivity;
import com.zmx.patients.view.activity.DoctorDetailsActivity;
import com.zmx.patients.view.activity.ExpertConsultationActivity;
import com.zmx.patients.view.activity.FamousDoctorHallActivity;
import com.zmx.patients.view.activity.FreeClinicActivity;
import com.zmx.patients.view.activity.HealthActivity;
import com.zmx.patients.view.activity.HealthDetectionActivity;
import com.zmx.patients.view.activity.IntelligentChatActivity;
import com.zmx.patients.view.activity.LiveActivity;
import com.zmx.patients.view.activity.LoginActivity;
import com.zmx.patients.view.activity.MakeAppointmentActivity;
import com.zmx.patients.view.activity.MallMedicineActivity;
import com.zmx.patients.view.activity.MeBangleActivity;
import com.zmx.patients.view.activity.MessageActivity;
import com.zmx.patients.view.activity.PatientsCircleActivity;
import com.zmx.patients.view.activity.PrescribingActivity;
import com.zmx.patients.view.activity.TCMKnowledgeActivity;
import com.zmx.patients.view.activity.ThePreventionActivity;
import com.zmx.patients.view.activity.TongueCoatingActivity;
import com.zmx.patients.view.activity.UpdateIntelligentChatActivity;
import com.zmx.patients.view.activity.WindowOfSoulActivity;
import com.zmx.patients.view.util.CustomTextSwitcher;
import com.zmx.patients.view.util.CustomToast;
import com.zmx.patients.view.util.GlideImageLoader;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.app.Activity.RESULT_OK;

/**
 * 首页提交了
 */
public class HomeFragment extends SimpleImmersionFragment {

    private RelativeLayout relative_a,data_relative,laba,relative1, relative2, relative3, relative4, relative5, relative6,relative7, relative8, zhibo,zhibo1,liLayout1,liLayout2,liLayout3;
    private LinearLayout no_data;
    private TextView data_view,hr,l_More,tm,td,more_doctor;

//    private ArticleMessageAdapter adapter;
//    private List<ArticleMessage> lists;
//    private RecyclerView re_doctor;
//需要的权限数组 读/写/相机
private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};

    private int REQUEST_CODE_SCAN = 1;
    private RecyclerView recycler_view;
    private RecommendedDoctorAdapter rdadapter;
    private List<Doctor> doctors;

    private EditText search;

    private BraceletData bd;

    private CustomTextSwitcher tvDownUpTextSwitcher;

    private ImageView image_fz;


    private TextView message_button;

    private static BadgeTextView mBadgeView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        //接口InformUpdateListener的实现方法，监听数据更新中是否有首页图片更新
        InformUpdateListenerManager informUpdateListenerManager = new InformUpdateListenerManager(this.getActivity());
        informUpdateListenerManager.getInstance(this.getActivity()).setInformUpdateListener(new MyInformUpdateListener(this.getActivity()));

        recycler_view = view.findViewById(R.id.recycler_view);
        rdadapter = new RecommendedDoctorAdapter(this.getActivity());
        LinearLayoutManager layout = new LinearLayoutManager(HomeFragment.this.getActivity());
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);//设置为横向排列
        recycler_view.setLayoutManager(layout);
        recycler_view.setAdapter(rdadapter);
        no_data = view.findViewById(R.id.no_data);
        data_relative = view.findViewById(R.id.data_relative);

        no_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    //页面返回时，弹出提示框，包括确认、取消按钮，提示文字
                    AlertDialog.Builder builder = new AlertDialog.Builder(HomeFragment.this.getContext());
                    builder.setTitle("选择添加手环方式");
                    builder.setNegativeButton("手动填写设备号", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            alert_label("输入设备号");

                        }
                    });
                    builder.setPositiveButton("扫描二维码", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //检查是否已经获得相机的权限
                            if (verifyPermissions(HomeFragment.this.getActivity(), PERMISSIONS_STORAGE[2]) == 0) {
                                ActivityCompat.requestPermissions(HomeFragment.this.getActivity(), PERMISSIONS_STORAGE, 3);
                            } else {
                                //已经有权限
                                Intent intent = new Intent(HomeFragment.this.getActivity(), CaptureActivity.class);
                                /*ZxingConfig是配置类
                                 *可以设置是否显示底部布局，闪光灯，相册，
                                 * 是否播放提示音  震动
                                 * 设置扫描框颜色等
                                 * 也可以不传这个参数
                                 * */
                                ZxingConfig config = new ZxingConfig();
                                config.setPlayBeep(true);//是否播放扫描声音 默认为true
                                config.setShake(true);//是否震动  默认为true
                                config.setDecodeBarCode(true);//是否扫描条形码 默认为true
                                config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
                                config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
                                config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
                                config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                                intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                                startActivityForResult(intent, REQUEST_CODE_SCAN);
                            }
                        }
                    });
                    builder.show();

                }

            }
        });

        if (MyApplication.isLogin_state()) {

            getInfo();

        }

        //走马灯
        tvDownUpTextSwitcher = view.findViewById(R.id.tvDownUpTextSwitcher);
        String[] data = new String[]{"腰椎间盘突出治疗方法选择","聊聊ICL术后那些事","早泄和阳痿合并的诊治","怎么判断自己有没有心理疾病"};
        // 上下滚动
        tvDownUpTextSwitcher.setInAnimation(R.anim.animation_down_up_in_animation)
                .setOutAnimation(R.anim.animation_down_up_out_animation)
                .bindData(data)
                .startSwitch(2000L);

        data_view = view.findViewById(R.id.more_view);
        more_doctor = view.findViewById(R.id.more_doctor);
        hr = view.findViewById(R.id.data_hr);
        tm = view.findViewById(R.id.data_tm);
        td = view.findViewById(R.id.data_td);
        data_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), HealthDetectionActivity.class);
                    startActivity(intent);

                }
            }
        });

        laba = view.findViewById(R.id.laba);
        laba.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LiveActivity.class);
                    startActivity(intent);

                }
            }
        });
        l_More = view.findViewById(R.id.l_More);
        l_More.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), LiveActivity.class);
                startActivity(intent);
            }
        });

        more_doctor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), FamousDoctorHallActivity.class);
                    startActivity(intent);

                }
            }
        });

        message_button = view.findViewById(R.id.message_button);
        mBadgeView  = new BadgeTextView(this.getContext());

        mBadgeView.setTargetView(message_button);//设置目标targetView(可任意:此处只做演示)
        mBadgeView.setBadgeCount(0)//数量(0:表示小红点)
                .setmDefaultRightPadding(10)//右依附视图距离
                .setmDefaultTopPadding(10);//上依附视图距离

        if (MyApplication.unread_message()) {

            mBadgeView.setVisibility(View.VISIBLE);

        }else{

            mBadgeView.setVisibility(View.GONE);
        }

        message_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    mBadgeView.setVisibility(View.GONE);
                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), MessageActivity.class);
                    startActivity(intent);

                }
            }
        });


        relative_a = view.findViewById(R.id.relative7);
        relative_a.setFocusable(false);
        relative_a.setOnClickListener(v -> {

//            if (!MyApplication.isLogin_state()) {
//
//                Intent intent = new Intent();
//                intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
//                startActivityForResult(intent, 10);
//
//            } else {
//
//                Intent intent = new Intent();
//                intent.setClass(HomeFragment.this.getContext(), WindowOfSoulActivity.class);
//                startActivity(intent);
//
//            }

        });

        rdadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    String did = doctors.get(listPosition).getMember_id() + "";
                    //目前Activity→目标Activity
                    Intent intent = new Intent(HomeFragment.this.getContext(), DoctorDetailsActivity.class);
                    intent.putExtra("doctor_id", did);
                    intent.putExtra("class_id", doctors.get(listPosition).getClass_id() + "");
                    intent.putExtra("hosp_id", doctors.get(listPosition).getHosp_id() + "");
                    startActivity(intent);

                }


            }
        });
        doctors = new ArrayList<>();

        zhibo = view.findViewById(R.id.zhibo);
        zhibo.setOnClickListener(v -> {

            Intent intent = new Intent();
            intent.setClass(HomeFragment.this.getContext(), LiveActivity.class);
            startActivity(intent);

        });
        zhibo1 = view.findViewById(R.id.zhibo1);
        zhibo1.setOnClickListener(v -> {

            Intent intent = new Intent();
            intent.setClass(HomeFragment.this.getContext(), LiveActivity.class);
            startActivity(intent);

        });

        search = view.findViewById(R.id.search);
        search.setFocusable(false);
        search.setOnClickListener(v -> {

            Intent intent = new Intent();
            intent.setClass(HomeFragment.this.getContext(), DiseaseSearchActivity.class);
            startActivity(intent);

        });

        liLayout1 = view.findViewById(R.id.liLayout_a);
        liLayout1.setFocusable(false);
        liLayout1.setOnClickListener(v -> {

            if (!MyApplication.isLogin_state()) {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                startActivityForResult(intent, 10);

            } else {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), AllDoctorActivity.class);
                startActivity(intent);

            }

        });

        liLayout3 = view.findViewById(R.id.liLayout_c);
        liLayout3.setFocusable(false);
        liLayout3.setOnClickListener(v -> {

            Intent intent = new Intent();
            intent.setClass(HomeFragment.this.getContext(), UpdateIntelligentChatActivity.class);
//            intent.setClass(HomeFragment.this.getContext(), IntelligentChatActivity.class);
            startActivity(intent);

        });

//        zhibos.setOnClickListener(v -> {
//
//            if (!MyApplication.isLogin_state()) {
//
//                Intent intent = new Intent();
//                intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
//                startActivityForResult(intent, 10);
//
//            } else {
//
//                Intent intent = new Intent();
//                intent.setClass(HomeFragment.this.getContext(), ExpertConsultationActivity.class);
//                startActivity(intent);
//
//            }
//
//        });

        liLayout2 = view.findViewById(R.id.liLayout_b);
        liLayout2.setOnClickListener(v -> {

            if (!MyApplication.isLogin_state()) {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                startActivityForResult(intent, 10);

            } else {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), MakeAppointmentActivity.class);
                startActivity(intent);

            }

        });

        relative1 = view.findViewById(R.id.relative1);
        relative1.setOnClickListener(v -> {

            if (!MyApplication.isLogin_state()) {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                startActivityForResult(intent, 10);

            } else {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), DiseaseLibraryActivity.class);
                startActivity(intent);

            }


        });

        relative2 = view.findViewById(R.id.relative2);
        relative2.setOnClickListener(v -> {

            if (!MyApplication.isLogin_state()) {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                startActivityForResult(intent, 10);

            } else {
                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), TCMKnowledgeActivity.class);
                startActivity(intent);
            }


        });

        relative3 = view.findViewById(R.id.relative3);
        relative3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), MallMedicineActivity.class);
                    startActivity(intent);
                }


            }
        });

        relative4 = view.findViewById(R.id.relative4);
        relative4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("点击了", "点击了");

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), CircleOfFriendsActivity.class);
                    startActivity(intent);

                }


            }
        });

        relative5 = view.findViewById(R.id.relative5);
        relative5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), CommunityActivity.class);
                    startActivity(intent);

                }


            }
        });


        relative6 = view.findViewById(R.id.relative6_a);
        relative6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), HealthActivity.class);
                    startActivity(intent);

                }

            }
        });


        relative7 = view.findViewById(R.id.relative7);
        relative7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), HealthDetectionActivity.class);
                    startActivity(intent);

                }
            }
        });

        image_fz = view.findViewById(R.id.image_fz);
        image_fz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent();
                intent.setClass(HomeFragment.this.getContext(), ThePreventionActivity.class);
                startActivity(intent);

            }
        });


        relative8 = view.findViewById(R.id.relative8);
        relative8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!MyApplication.isLogin_state()) {

                    Intent intent = new Intent();
                    intent.setClass(HomeFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    Intent intent = new Intent();
//                    intent.setClass(HomeFragment.this.getContext(), COVIDActivity.class);
                    intent.setClass(HomeFragment.this.getContext(), TongueCoatingActivity.class);
                    startActivity(intent);


                }
            }
        });

//        adapter = new ArticleMessageAdapter(this.getActivity());
//        re_doctor = view.findViewById(R.id.re_doctor);
//        re_doctor.setLayoutManager(new LinearLayoutManager(this.getContext()));
//        re_doctor.setAdapter(adapter);
//        lists = new ArrayList<>();
//        adapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            protected void onItemClick(View view, int listPosition) {
//
//                //目前Activity→目标Activity
//                Log.e("范德萨发发", "放到" + lists.size());
//                Log.e("范德萨发发", "放到" + lists.get(listPosition).getTitle());
//                Intent intent = new Intent(HomeFragment.this.getContext(), ArticleDetailsActivity.class);
//                intent.putExtra("am", lists.get(listPosition));
//                startActivity(intent);
//
//            }
//        });
//        getAllDoctor();
        getAllDoctors();
        getInfo();
        return view;
    }

    /**
     * 检查是否有对应权限
     *
     * @param activity   上下文
     * @param permission 要检查的权限
     * @return 结果标识
     */
    public int verifyPermissions(Activity activity, java.lang.String permission) {
        int Permission = ActivityCompat.checkSelfPermission(activity, permission);
        if (Permission == PackageManager.PERMISSION_GRANTED) {

            return 1;
        } else {
            return 0;
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        CustomToast.getInstance().showToastCustom(HomeFragment.this.getContext(), object.getString("msg"), Gravity.BOTTOM);

                        if (object.getInt("code") == 1){
                            getInfo();
                        }



                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
                case 2:

                    Log.e("内容", "" + msg.obj.toString());
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(msg.obj.toString());
                        JSONArray array = jsonObject.getJSONArray("data");
                        Gson g = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject object = array.getJSONObject(i);
                            Doctor c = g.fromJson(object.toString(), Doctor.class);
                            doctors.add(c);

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    rdadapter.setListAndNotifyDataSetChanged(doctors);//就是设置list然后刷新
                    break;

                case 3:

                    try {

                        JSONObject jb = new JSONObject(msg.obj.toString());

                        if (jb.getInt("code") == 1) {

                            no_data.setVisibility(View.GONE);
                            data_relative.setVisibility(View.VISIBLE);
                            JSONArray array = jb.getJSONArray("data");
                            Gson gson = new Gson();
                            for (int i = 0; i < array.length(); i++) {

                                bd = gson.fromJson(array.getJSONObject(i).toString(), BraceletData.class);

                            }

                            if(bd != null){

                                td.setText(bd.getBp()+"");
                                tm.setText(bd.getBt()+"");
                                hr.setText(bd.getHr()+"/120");

                            }

                        }else{

                            no_data.setVisibility(View.VISIBLE);
                            data_relative.setVisibility(View.GONE);
                            data_view.setVisibility(View.GONE);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this).titleBarMarginTop(R.id.toolbar) .statusBarDarkFont(true).init();
    }


    public void getAllDoctors() {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", "getAllDoctor");
        params.put("data[row]", 1 + "");
        params.put("data[number]", 10 + "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/api/", params, handler, 2, 404);
    }



    //获取血压数据
    public void getInfo() {
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getInfo");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch/", params, handler, 3, 404);

    }

    //签名
    public String sing(List<String> list) {

        StringBuilder requestparams = new StringBuilder();
        requestparams.append(MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, "") +
                MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.role, "") +
                System.currentTimeMillis() / 1000 + MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.password, ""));

        if (list != null) {
            for (String s : list) {
                requestparams.append(s);
            }
        }
        return requestparams.toString();

    }

    //静态内部类实现接口监听
    static class MyInformUpdateListener implements MessageUpdateListener {
        private WeakReference<Activity> mWeakReference;
        public MyInformUpdateListener(Activity activity){
            Log.e("有消息进来了1","有消息进来了1");
            mWeakReference = new WeakReference<Activity>(activity);//使用弱引用引用外部Activity对象
        }

        @Override
        public void updateMessage() {

            Log.e("有消息进来了","有消息进来了");
            mBadgeView.setVisibility(View.VISIBLE);

        }
    }

    public void alert_label(String text) {

        AlertDialog.Builder builder = new AlertDialog.Builder(HomeFragment.this.getContext());

        LayoutInflater factory = LayoutInflater.from(HomeFragment.this.getContext());//提示框
        final View view = factory.inflate(R.layout.dialog_edit, null);//这里必须是final的
        final EditText et = view.findViewById(R.id.editText);
        et.setHint(text);
        builder.setTitle(text);

        final AlertDialog dialog = builder.create();
        dialog.setView(view);

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(et.getText().toString())) {

                    binding(et.getText().toString());
                    dialog.dismiss();

                } else {

                    CustomToast.getInstance().showToastCustom(HomeFragment.this.getContext(), "非法输入", Gravity.BOTTOM);

                }

            }
        });


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {

                String content = data.getStringExtra(Constant.CODED_CONTENT);
                binding(content);

        }

    }

    public void binding(String devicel) {

        List<String> l = new ArrayList<>();
        l.add(devicel);
        l.add(MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, ""));

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "binding");
        params.put("data[devicel]", devicel);
        params.put("data[patient_id]", MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, ""));
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch/", params, handler, 1, 404);

    }


    @Override
    public void onResume() {
        super.onResume();

        if (MyApplication.isLogin_state()) {

            getInfo();

        }else {
            no_data.setVisibility(View.VISIBLE);
            data_relative.setVisibility(View.GONE);
            data_view.setVisibility(View.GONE);
        }

    }


}